<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="产品标题:" prop="title.value">
                            <el-input v-model="queryParams.title.value" placeholder="请输入产品标题" clearable />
                        </el-form-item>
                    </el-col> -->
                    <el-col :md="6" :sm="12">
                        <el-form-item label="编号:" prop="brand.value">
                            <el-input v-model="queryParams.brand.value" placeholder="请输入编号" clearable />
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="备注名称:" prop="remarks.value">
                            <el-input v-model="queryParams.remarks.value" placeholder="请输入备注名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="尺码表生成状态" prop="table_status.value">
                            <el-select v-model="queryParams.table_status.value" placeholder="请选择">
                                <el-option label="已生成" value="1" />
                                <el-option label="未生成" value="0" />
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :md="12" :sm="12">
                        <el-form-item label="产品标签:" prop="tags.value">
                            <el-select v-model="queryParams.product_tags.value" multiple placeholder="请输入产品标签">
                                <el-option
                                v-for="item in tags"
                                :key="item.value"
                                :label="item.value"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="click_update" v-hasPermi="api.click_update">手动更新</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <SelectPage v-model="shop" :url="'/admin/site/selectPage'" show_field="site_name" placeholder="请选择新店铺" query_field="site_name" :multiple="false" />
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" v-loading="exportLoading" size="mini" @click="export_product">直接导出</el-button>
                </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

    <el-tabs v-model="activeTab" @tab-click="handleStatus" type="border-card">
        <el-tab-pane v-for="(item, index) in status" :key="index" :label="item.label" :name="item.value.toString()">
            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="240" align="left">
                        <template slot-scope="scope">
                            <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button> -->
                            <el-button class="btn-mar" size="mini" type="info" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button>
                            <el-button class="btn-mar" size="mini" type="primary" @click="handleSkuEdit(scope.row)" v-hasPermi="api.get_sku">生成sku</el-button>
                            <el-button class="btn-mar" size="mini" type="primary" @click="tf_apply(scope.row)"  v-hasPermi="api.tf_apply" >投放申请</el-button>
                            <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
        </el-tab-pane>
    </el-tabs>
            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen" :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData" :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

        <!-- sku弹窗 -->
        <el-dialog :title="!disabled ? 'sku修改' : '详情'" :visible="skuOpen" :before-close="beforeSkuClose" destroy-on-close>
            <EditSkuForm ref="editSkuForm" :findFormData="findFormData" :disabled="disabled" @submitSkuForm="submitSkuForm" :editFormParams="editFormParams" @editSkuFormCancel="beforeSkuClose" />
        </el-dialog>

        <el-dialog :title="!disabled ? '详情' : '详情'" :visible="Opentf_apply" :before-close="beforetf_applyClose" destroy-on-close>
            <Edittf_applyForm ref="edittf_applyForm" :findFormData="findFormData" :disabled="disabled" @submittf_applyForm="submittf_applyForm" :editFormParams="editFormParams" @edittf_applyFormCancel="beforetf_applyClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
import EditSkuForm from './EditSkuFormCom'
import Edittf_applyForm from './Edittf_applyFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm, EditSkuForm,Edittf_applyForm },

    data() {
        return {
            shop: 0,

            skuOpen: false,
            Opentf_apply: false,
            // table结构
            columns: [
                // {
                //     visible: true,
                //     label: '产品标题',
                //     prop: 'title',
                // },
                {
                    visible: true,
                    label: '编号',
                    prop: 'brand',
                },
                // {
                //     visible: true,
                //     label: '产品分类',
                //     prop: 'flag',
                //     width: 180,
                //     component: 'QuickAdminTag',
                //     formatter: (prop, row) => {
                //         return row.product_type
                //     },
                // },
                // {
                //     visible: true,
                //     label: '产品标签',
                //     prop: 'flag',
                //     width: 180,
                //     component: 'QuickAdminTag',
                //     formatter: (prop, row) => {
                //         return row.tags
                //     },
                // // },
                // {
                //     visible: true,
                //     label: '备注名称',
                //     prop: 'remarks',
                // },
                {
                    visible: true,
                    label: '图片',
                    prop: 'images',
                    component: 'QuickAdminImage',
                },

                {
                    visible: true,
                    label: '投放开发人员',
                    prop: 'dev_admin',
                },
                {
                    visible: true,
                    label: '在投人数',
                    prop: 'tf_num',
                },
                {
                    visible: true,
                    label: '投放状态',
                    prop: 'is_tf',
                },
               
            ],
            // 接口地址
            api: {
                index: `/admin/product/index`,
                find: `/admin/product/find`,
                add: `/admin/product/add`,
                edit: `/admin/product/edit`,
                get_sku: `/admin/index/get_sku`,
                delete: `/admin/product/delete`,
                export: `/admin/product/export`,
                status: `/admin/product/status`,
                tf_apply: `/admin/tfrecord/tf_apply`,
                // tf_apply_brand: `/admin/tfrecord/tf_apply_brand`,
                export_product: `/admin/product/copy_product_to_shopify`,
            },
            editFormParams: {},
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
            //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                id: { value: undefined, op: '=' },
                title: { value: undefined, op: '%like%' },
                brand: { value: undefined, op: '%like%' },
                remarks: { value: undefined, op: '%like%' },
                product_tags: { value: undefined, op: '%like%' },
                table_status: { value: undefined, op: '=' },
                product_status: { value: undefined, op: '=' },
            },
            status: [
                { label: '样品库', value: '' },
                // { label: 'Full Coverage', value: 'Full Coverage' },
                // { label: 'Shapewear', value: 'Shapewear' },
                // { label: 'Sports', value: 'Sports' },
                // { label: 'Wireless', value: 'Wireless' },
            ],

            tags:[
                {
                    value: 'Back Support',
                },{
                    value: 'Full Coverage',
                },{
                    value: 'Shapewear',
                },{
                    value: 'Wireless',
                },
            ],
            activeTab: '',
        }
    },
    created() {
        this.initIndex()
    },
    watch: {},
    methods: {
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },
        // sku生成
        handleSkuEdit(row) {
            this.handleSkuFind(row[this.primaryKey])
        },

        async tf_apply(row) {
            this.$confirm(`是否确认投放申请?`, '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                .then(() => {
                    this.handletf_apply(row.brand)
                })
                .then(() => {
                })
                .catch(() => {})
                .finally(() => {
                    
                })
        },
        // tf_apply(row) {
        //     this.handletf_apply(row.brand)
        // },
        async handletf_apply(brand) {
            let response = await this.request.post(this.api.tf_apply, { 
                        brand: brand,
                    })
            if(response.msg !== '申请成功'){
                this.$message.error(response.msg)
            }else{
                this.$message.success(response.msg)
            }
        },

        export_product(row) {
            let tableIds = row[this.primaryKey] || this.ids
            tableIds = tableIds.toString()
            if(tableIds == ''){
                this.$message.error('请勾选需要导出的商品');
                return false;
            }
            if(tableIds.length >= 29 ){
                this.$message.error('每次导出最多两个商品');
                return false;
            }
            if(this.shop == ''){
                this.$message.error('请选择导出的店铺');
                return false;
            }
            this.$confirm(`是否确认导出到新店铺?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
            .then(() => {
                this.exportLoading = true
                return this.request.post(this.api.export_product, { 
                    product_id: tableIds,
                    site_id:this.shop,
                })
            })
            .then(() => {
                this.exportLoading = false
                this.initIndex()
                this.msgSuccess('导出成功')
            })
            .catch(() => {})
            .finally(() => {
                this.exportLoading = false
            })
        },
        // sku生成回显
        async handleSkuFind(id) {
            let response = await this.request.get(this.api.get_sku, { params: { product_id: id } })
            this.findFormData = this.formatFindData(response.data)
            this.skuOpen = true
        },

        // 订单状态切换查询
        handleStatus(tab) {
            this.queryParams.product_status.value = tab.name
            this.submitSearch()
        },
    },
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
